import Vue from 'vue'
import App from './App.vue'
import store from './store'
import router from './router'
import { getToken, setToken } from '@/utils/auth'
import { getQueryVariable } from '@/utils/app'
import { login, getInfo } from '@/api/auth'
import fastClick from 'fastclick'
import Vant from 'vant';
import { Lazyload } from 'vant'
import 'vant/lib/index.css'
import '@/assets/style/common.css'
import { getActiveInfo } from '@/api/user'


// fastClick.attach(document.body)
Vue.config.productionTip = false
Vue.use(Vant)
Vue.use(Lazyload)
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  let token = getToken()
  
  let openid = getQueryVariable('openid')
  // next()
  if((openid || to.query.openid) && !token) {
    let data = {}
    if(openid) {
      data.openid = openid
    }else{
      data = to.query
    }
  	getInfo(data).then(res=>{
      if(res) {
        setToken(res.access_token)
        store.commit('SET_USER', res.user)
        getActiveInfo({id: 1}).then(res=> {
          store.commit('SET_ACTIVE', res)
        })
      }
      window.location.href = window.origin + process.env.VUE_APP_PATH
      next()
    })
  }else{
    if(!token) {
      console.log(window.location.href)
      window.location.href = 'https://palive.0512iis.com/oauth/redirect?url=' + encodeURIComponent(window.origin + process.env.VUE_APP_PATH)
    }else{
      getActiveInfo({id: 1}).then(res=> {
          store.commit('SET_ACTIVE', res)
      })
      next()
      
    }
  }
  
})
new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')
